package com.woniu.car.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.car.common.ResponseEntity;
import com.woniu.car.dto.ResponseCodeDto;
import com.woniu.car.dto.TProcessDto;
import com.woniu.car.dto.TProcessValueDto;
import com.woniu.car.dto.constant.CommonConstant;
import com.woniu.car.dto.TProcessValueQueryDto;
import com.woniu.car.exception.ParamException;
import com.woniu.car.param.TProcessParam;
import com.woniu.car.param.TProcessUpdateParam;
import com.woniu.car.param.TProcessValueUpdateParam;
import com.woniu.car.service.TProcessService;
import com.woniu.car.service.TProcessValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName AfterSalesController
 * @Description TODO
 * @Author 李磊
 * @Version 1.0
 **/
@RestController
@RequestMapping("/process")
@Api(tags = "服务流程模块")
@Slf4j
public class ProcessController {

    @Resource
    private TProcessService tProcessService;
    @Resource
    private TProcessValueService tProcessValueService;

    /**
    *@Author 李磊
    *@Description 服务流程新增
    *@Param [tProcessParam, id]
    *@return com.woniu.car.common.ResponseEntity
    **/
    @PostMapping("/insert")
    @ApiOperation("服务流程新增")
    public ResponseEntity insert(@Validated TProcessParam tProcessParam,@RequestHeader("id") Integer id) throws Exception{

        if (null == id) {
            throw new ParamException(CommonConstant.PARAMMESS);
        }

        TProcessDto insert = tProcessService.insert(tProcessParam, id);

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                insert);
    }

    /**
    *@Author 李磊
    *@Description 服务流程修改
    *@Param [tProcessParam, id]
    *@return com.woniu.car.common.ResponseEntity
    **/
    @PutMapping("/update")
    @ApiOperation("服务流程修改")
    public ResponseEntity update(@Validated TProcessUpdateParam tProcessUpdateParam, @RequestHeader("id") Integer id) throws Exception{

        if (null == id) {
            throw new ParamException(CommonConstant.PARAMMESS);
        }

        TProcessDto tProcessDto = tProcessService.update(tProcessUpdateParam,id);

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                tProcessDto);
    }

    /**
    *@Author 李磊
    *@Description 服务流程删除
    *@Param [processId, id]
    *@return com.woniu.car.common.ResponseEntity
    **/
    @PutMapping("/delete")
    @ApiOperation("服务流程删除")
    public ResponseEntity delete(Integer processId, @RequestHeader("id") Integer id) throws Exception{

        if (null == processId || processId <= 0 || null == id) {
            throw new ParamException(CommonConstant.DELNULL);
        }

        TProcessDto tProcessDto = tProcessService.delete(processId, id);

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                tProcessDto);
    }

    /**
    *@Author 李磊
    *@Description 查询所有接口
    *@Param []
    *@return com.woniu.car.common.ResponseEntity
    **/
    @GetMapping("/queryAll")
    @ApiOperation("查询所有服务流程")
    public ResponseEntity queryAll(){

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("status",CommonConstant.ZERO);

        List list = tProcessService.list(wrapper);

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                list);
    }

    /**
    *@Author 李磊
    *@Description 查询某一服务服务流程
    *@Param [serviceId]
    *@return com.woniu.car.common.ResponseEntity
    **/
    @GetMapping("/queryByService")
    @ApiOperation("查询某一服务服务流程")
    public ResponseEntity queryByService(Integer serviceId) throws Exception {

        List<TProcessDto> tProcessDtos = tProcessService.queryByService(serviceId);

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                tProcessDtos);
    }

    /**
    *@Author 李磊
    *@Description 下单创建服务流程
    *@Param [list]
    *@return com.woniu.car.common.ResponseEntity
    **/
    @PostMapping("/insertValue")
    @ApiOperation("下单创建服务流程")
    public ResponseEntity insertValue(Integer detailId, Integer id) throws Exception {

        if (null == detailId) {
            throw new ParamException(CommonConstant.PROCESSNULL);
        }

        List<TProcessValueDto> tProcessValueDtos = tProcessValueService.insertValue(detailId,id);

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                tProcessValueDtos);
    }

    /**
    *@Author 李磊
    *@Description 修改流程状态
    *@Param [tProcessValueUpdateParam, id]
    *@return com.woniu.car.common.ResponseEntity
    **/
    @PutMapping("/updateStatus")
    @ApiOperation("修改流程状态(服务进程)")
    public ResponseEntity updateStatus(@Validated TProcessValueUpdateParam tProcessValueUpdateParam, @RequestHeader("id") Integer id) throws Exception{

        if (null == id) {
            throw new ParamException(CommonConstant.PARAMMESS);
        }

        TProcessValueDto tProcessValueDto = tProcessValueService.updateValueStatus(tProcessValueUpdateParam, id);

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                tProcessValueDto);
    }

    /**
    *@Author 李磊
    *@Description 展示服务流程
    *@Param [detailId]
    *@return com.woniu.car.common.ResponseEntity
    **/
    @GetMapping("/queryProcessValue")
    @ApiOperation("展示服务流程")
    public ResponseEntity queryProcessValue(Integer detailId){

        if (null == detailId) {
            throw new ParamException(CommonConstant.PARAMMESS);
        }

        List<TProcessValueQueryDto> tProcessValueQueryDtos = tProcessValueService.queryProcessValue(detailId);

        return new ResponseEntity(ResponseCodeDto.SUCCESS.getCode(),
                ResponseCodeDto.SUCCESS.getMessage(),
                tProcessValueQueryDtos);
    }

}
